<?php

namespace App\Validation;

use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\Numericality;

class CitySearchValidation extends BaseValidation
{
	public function initialize()
	{
		$this->add(
			"is_hot",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
				  ]
			)
		);

		$this->add(
			"p",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
				  ]
			)
		);

		$this->add(
			"is_group",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
				  ]
			)
		);
		
		$this->add(
			"page_num",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
				  ]
			)
		);
		$this->add(
			"city",
			new PresenceOf(
				  [
					  "message" => ":field is required",
					  "allowEmpty" => true,
				  ]
			)
		);
	}

	//验证之前
	public function beforeValidation($data, $entity, $messages)
    {
    	parent::beforeValidation($data, $entity, $messages);
    	
    	$data->is_hot = property_exists($data,'is_hot') ? $data->is_hot : -1;
    	$data->is_group = property_exists($data,'is_group') ? $data->is_group : 1;
    	$data->city = property_exists($data,'city') ? trim($data->city) : NULL;

        return true;
    }
}